package wypozyczalnia.forms;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;
import org.primefaces.model.UploadedFile;


public class EditPersonForm {

	private Integer id;
	private String name;
	private String surname;
	private String description;
	private String nationality;
	private Date birthDate;
	
	private List<String> listImages;
	private Map<UUID, UploadedFile> uploadedFiles;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getSurname() {
		return surname;
	}

	public void setSurname(String surname) {
		this.surname = surname;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getNationality() {
		return nationality;
	}

	public void setNationality(String nationality) {
		this.nationality = nationality;
	}

	public Date getBirthDate() {
		return birthDate;
	}

	public void setBirthDate(Date birthDate) {
		this.birthDate = birthDate;
	}

	public List<String> getListImages() {
        return listImages;
    }
	
	public void addPhoto(UploadedFile uploadedFile){
		 final UUID uuid = UUID.randomUUID();
		 if(uploadedFiles==null){
			 uploadedFiles = new HashMap<UUID, UploadedFile>();
		 }
         uploadedFiles.put(uuid, uploadedFile);
         if(listImages == null){
        	 listImages = new ArrayList<String>();
         }
         listImages.add(uuid.toString());
	}

    public StreamedContent getImage() {
        final ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
        final String imageId = (String) externalContext.getRequestParameterMap().get("imageId");
        StreamedContent image= null;
        if (imageId != null) {
            final UUID imageIndex = UUID.fromString(imageId);
            image = new DefaultStreamedContent(new ByteArrayInputStream(uploadedFiles.get(imageIndex).getContents()), "image/png");
            return image;
        }

        return null;
    }


}
